﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zo.Models;
using ZoLone.Models.Sys;

namespace ZoLone.Logics.Sys
{
    public class OperationLogic : BaseLogic
    {
        /// <summary>
        /// 根据条件获取权限
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<Sys_Operation> GetOperationList(Func<Sys_Operation,bool> action=null)
        {
            if (action!=null)
            {
                var result= db.Sys_Operation.Where(action).ToList();
                return result;
            }
            else
            {
                var result = db.Sys_Operation.ToList();
                return result;
            }
          
        }
        /// <summary>
        /// 获取权限列表分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<Sys_Operation> GetOperationListByPage(int pageIndex, int pageSize, out int total)
        {
            var query = db.Sys_Operation;
            total = query.Count();
            return query.Skip(pageIndex * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 获取单条记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sys_Operation GetOperaionById(int id)
        {
            var result = db.Sys_Operation.Where(c => c.Id.Equals(id)).FirstOrDefault();
            return result;
        }

        /// <summary>
        /// 保存权限
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        public int SaveRight(Sys_Operation bo)
        {
            if (bo.Id == 0)
            {
                if (bo.ParentId == 0)
                {
                    bo.IsMenu = (int)Sys_Operation.IsMenuState.yes;
                }
                else
                {
                    //三级内全部是菜单
                    int p = db.Sys_Operation.Where(c => c.Id.Equals(bo.ParentId)).Select(c => c.ParentId).SingleOrDefault();
                    if (p == 0)
                    {
                        bo.IsMenu = (int)Sys_Operation.IsMenuState.yes;
                    }
                    else
                    {
                        bo.IsMenu = (int)Sys_Operation.IsMenuState.no;
                    }
                }
                db.Sys_Operation.Add(bo);
                db.SaveChanges();
                return bo.Id;
            }
            else
            {
                var op = db.Sys_Operation.Where(c => c.Id.Equals(bo.Id)).SingleOrDefault();
                //如果不是顶级点击按钮，action不能填
                if (op.ParentId == 0 && !string.IsNullOrEmpty(bo.Right) && bo.IsTopClickMenu == (int)Sys_Operation.IsTop.no)
                {
                    throw new ErrorBox("If it is not TopClickMenu,the action must be null!");
                }
                if (bo.IsTopClickMenu == (int)Sys_Operation.IsTop.yes)
                {
                    if (op.ParentId != 0 || string.IsNullOrEmpty(bo.Right))
                    {
                        throw new ErrorBox("Must be a top menu to set clicks and the menu must set Action right!");
                    }
                    //有子级不可设为点击菜单
                    var child_count = db.Sys_Operation.Where(c => c.ParentId == op.Id).Count();
                    if (child_count > 0)
                    {
                        throw new ErrorBox("Menus with children cannot be set as top-level click menus");
                    }
                }
                op.Id = bo.Id;
                op.Name = bo.Name;
                op.Right = bo.Right;
                op.IsTopClickMenu = bo.IsTopClickMenu;
                op.Icon = bo.Icon;

                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 获取权限树
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Sys_Operation> GetOperationTree()
        {
            var menus = GetOperationList();
            var parents = menus.Where(c => c.ParentId == 0);
            //一级权限菜单
            foreach (var menu in parents)
            {
                //二级权限菜单
                var chi = menus.Where(c => c.ParentId == menu.Id).ToList();
                //三级权限菜单
                foreach (var ci in chi)
                {
                    ci.Child = new List<Sys_Operation>();
                    ci.Child.AddRange(menus.Where(c => c.ParentId == ci.Id));
                }
                menu.Child = new List<Sys_Operation>();
                menu.Child.AddRange(chi);
            }
            return parents;
        }

        /// <summary>
        /// 获取权限树，并遍历已有的权限
        /// </summary>
        /// <param name="ops"></param>
        /// <returns></returns>
        public IEnumerable<Sys_Operation> GetOperationTree(List<int> ops)
        {
            var menus = GetOperationList();
            var parents = menus.Where(c => c.ParentId == 0);
            //一级权限菜单
            foreach (var menu in parents)
            {
                //二级权限菜单
                var chi = menus.Where(c => c.ParentId == menu.Id).ToList();

                foreach (var ci in chi)
                {
                    ci.Child = new List<Sys_Operation>();
                    //三级权限菜单
                    var xi = menus.Where(c => c.ParentId == ci.Id);
                    foreach (var x in xi)
                    {
                        if (ops.Contains(x.Id))
                        {
                            x.Check = true;
                        }
                    }
                    ci.Child.AddRange(xi);
                    if (ops.Contains(ci.Id))
                    {
                        ci.Check = true;
                    }
                }
                menu.Child = new List<Sys_Operation>();
                menu.Child.AddRange(chi);
                if (ops.Contains(menu.Id))
                {
                    menu.Check = true;
                }
            }
            return parents;
        }
    }
}
